import riceService from '@/services/rice-service';
import { ArrowLeftOutlined } from '@ant-design/icons';
import { Button, Card, Descriptions, Divider, Table, Tag, message } from 'antd';
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';

const RiceVarietyDetail = () => {
  const { id } = useParams();
  const navigate = useNavigate();
  const [loading, setLoading] = useState(true);
  const [varietyData, setVarietyData] = useState(null);

  const approvalColumns = [
    {
      title: '审定编号',
      dataIndex: 'approval_number',
      key: 'approval_number',
    },
    {
      title: '申请者',
      dataIndex: 'applicant',
      key: 'applicant',
    },
    {
      title: '育种者',
      dataIndex: 'breeder',
      key: 'breeder',
    },
    {
      title: '品种来源',
      dataIndex: 'origin',
      key: 'origin',
      ellipsis: true,
    },
  ];

  useEffect(() => {
    const fetchVarietyDetail = async () => {
      try {
        const response = await riceService.varieties_detail({
          variety_id: id,
        });
        setVarietyData(response.data.data);
      } catch (error) {
        message.error('获取品种详情失败');
        navigate('/dataManager/RiceVarietiesList');
      } finally {
        setLoading(false);
      }
    };

    fetchVarietyDetail();
  }, [id, navigate]);

  const getTypeTag = (type) => {
    if (!type) return null;

    let color = 'geekblue';
    if (type.includes('籼型')) color = 'volcano';
    if (type.includes('粳型')) color = 'green';
    if (type.includes('旱')) color = 'orange';
    if (type.includes('糯')) color = 'purple';

    return <Tag color={color}>{type}</Tag>;
  };

  if (loading) {
    return <div className="p-4">加载中...</div>;
  }

  if (!varietyData) {
    return <div className="p-4">未找到该品种</div>;
  }

  return (
    <div className="p-4">
      <Button
        type="text"
        icon={<ArrowLeftOutlined />}
        onClick={() => navigate('/dataManager/RiceVarietiesList')}
        className="mb-4"
      >
        返回列表
      </Button>

      <Card
        title={varietyData.name}
        bordered={false}
        extra={getTypeTag(varietyData.variety_type)}
      >
        <Descriptions bordered column={2}>
          <Descriptions.Item label="品种ID">
            {varietyData.seed_id}
          </Descriptions.Item>
          <Descriptions.Item label="父本">
            {varietyData.father_parent || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="母本">
            {varietyData.mother_parent || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="亲本来源">
            {varietyData.parent_source || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="选育单位" span={2}>
            {varietyData.breeding_unit || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="其他信息" span={2}>
            {varietyData.other_info || '-'}
          </Descriptions.Item>
          <Descriptions.Item label="全部" span={2}>
            {varietyData.all_data || '-'}
          </Descriptions.Item>
        </Descriptions>

        <Divider orientation="left">审定信息</Divider>

        {varietyData.approvals && varietyData.approvals.length > 0 ? (
          <Table
            columns={approvalColumns}
            dataSource={varietyData.approvals}
            rowKey="approval_number"
            pagination={false}
            bordered
          />
        ) : (
          <div className="text-gray-500">暂无审定信息</div>
        )}

        {varietyData.approvals?.map((approval, index) => (
          <div key={index} className="mt-6">
            <h3 className="text-lg font-medium mb-2">
              {approval.approval_number}
            </h3>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              <Card title="特征特性" size="small">
                <div className="whitespace-pre-line">
                  {approval.characteristics || '-'}
                </div>
              </Card>
              <Card title="产量表现" size="small">
                <div className="whitespace-pre-line">
                  {approval.yield_performance || '-'}
                </div>
              </Card>
              <Card title="栽培技术要点" size="small">
                <div className="whitespace-pre-line">
                  {approval.cultivation_techniques || '-'}
                </div>
              </Card>
              <Card title="审定意见" size="small">
                <div className="whitespace-pre-line">
                  {approval.approval_opinion || '-'}
                </div>
              </Card>
              <Card title="其他信息" size="small">
                <div className="whitespace-pre-line">
                  {approval.other_info || '-'}
                </div>
              </Card>
              <Card title="全部信息" size="small">
                <div className="whitespace-pre-line">
                  {approval.all_data || '-'}
                </div>
              </Card>
            </div>
          </div>
        ))}
      </Card>
    </div>
  );
};

export default RiceVarietyDetail;
